//
// Created by kongkong on 16/10/1.
// Copyright (c) 2016 Kongkong. All rights reserved.
//

#import "TopupViewController.h"

@interface TopupViewController()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView* tableView;
@property(nonatomic,strong) NSArray* data;
@property(nonatomic,strong) NSMutableArray* detailData;
@property(nonatomic,strong) UIButton*   submit;
@property (nonatomic,weak) UITextField* edit;
@property (nonatomic,weak) UISwitch* depositFlag;
@end


@implementation TopupViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"充值";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.submit];
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        
        _tableView.height = 400;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


- (UIButton *)submit{
    if (!_submit) {
        _submit = [[UIButton alloc]initWithFrame:CGRectMake(100, self.tableView.bottom+30, self.view.width-200, 30)];
        [_submit setTitle:@"充值" forState:UIControlStateNormal];
        _submit.backgroundColor = [UIColor orangeColor];
        [_submit addTarget:self action:@selector(topup) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submit;
}

- (void)loadData{
    self.data = @[@"当前充值设备 ID",@"当前机器余额",@"账户余额",@"充值金额",@"校准"];
    self.detailData = [@[@"_id_",@"1005",@"108",@"_edit_",@"_switch_off"]mutableCopy];
}

#pragma -mark tableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = self.data[indexPath.row];
    cell.accessoryView = nil;
    
    NSString* detail = self.detailData[indexPath.row];
    
    if ([detail hasPrefix:@"_"]) {
        if ([detail hasPrefix:@"_id_"]) {
            cell.detailTextLabel.text = [[FantasiaStorage instance]memoryValueForKey:@"currentDevice"];
        }
        else if ([detail hasPrefix:@"_edit_"]) {
            UITextField* edit = [[UITextField alloc]initWithFrame:CGRectMake(0, 8, tableView.width/3, cell.height-16)];
            edit.layer.borderColor = [UIColor darkGrayColor].CGColor;
            edit.layer.borderWidth = 1;
            edit.keyboardType = UIKeyboardTypeNumberPad;
            cell.accessoryView = edit;
            
            self.edit = edit;
        }
        else if ([detail hasPrefix:@"_switch_"]) {
            UISwitch* switchBtn = [[UISwitch alloc]init];
            NSString* isOn = [detail substringFromIndex:8];
            switchBtn.on = [isOn isEqualToString:@"on"];
            
            cell.accessoryView = switchBtn;
            self.depositFlag = switchBtn;
        }
    }
    else{
        cell.detailTextLabel.text = detail;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma -mark 充值
- (void)topup{
    
    if (EMPTY_STR(self.edit.text)) {
        [MBProgressHUD showError:@"请输入充值金额"];
        return;
    }
    
    [[[KKRequestEngine api:@"deposit" action:@"TOPUP"]
      parameter:@{
                  @"deviceID": [[FantasiaStorage instance]memoryValueForKey:@"currentDevice"],
                  @"value":self.edit.text,
                  @"flag":self.depositFlag.isOn?@"1":@"0"}]
     async:^(KKResponse *_Nullable response, id _Nullable result) {
         if ([response isSuccess]){
             [MBProgressHUD showSuccess:@"充值成功"];
         }
         else{
             [MBProgressHUD showError:@"充值失败"];
         }
         
     }];
}
@end
